package listlogms;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub total_list_logms {
    my $self = shift;

    my $cl;
    my @cl0 = ();
    if ( defined( $self->{p}->{fAdmin} ) ) {
        $cl = "log_smail AS t1,
        users AS t2
       WHERE t1.userid = t2.id AND t2.name = ?";

        push( @cl0, $self->{p}->{fAdmin} );
    }
    else { $cl = 'log_smail'; }

    my $sth = $self->{dbh}->prepare("SELECT count(*) FROM $cl");
    $sth->execute(@cl0);
    $self->{p}->{total_number} = $sth->fetchrow_array();
    $sth->finish();

    if ( defined( $self->{p}->{fAdmin} ) ) {
        $self->{p}->{url} = '&fAdmin=' . $self->{p}->{fAdmin};
    }

    return $self;
}

sub page_list_logms {
    my $self = shift;

    $self->{p}->{file} = 'logms';

    $self->SUPER::page_list(@_);

    return $self;

}

sub return_list_logms {
    my $self = shift;

    my $sth = $self->{dbh}->prepare(
        'SELECT name
                         FROM users 
                         ORDER BY created
                             '
    );
    $sth->execute();

    my @admin = ();
    while ( my $href = $sth->fetchrow_hashref() ) {
        push( @admin, $href->{name} );
    }
    $sth->finish();

    if ( defined( $self->{p}->{fAdmin} ) ) {
        my ( $index, $index2, $found );

        for ( $index = 0 ; $index < @admin ; $index++ ) {

            if ( $admin[$index] eq $self->{p}->{fAdmin} ) {
                $index2 = $index;
                $found  = 1;
                last;
            }
        }

        if ($found) {

            splice @admin, $index2, 1;

            my $i = $#admin + 1;
            $admin[$i] = $self->{p}->{fAdmin};
        }
    }

    my @admin2 = map +{ admin => $_, admin2 => $_ }, @admin;

#############

    my @ds = ();
    my $cl;
    if ( defined( $self->{p}->{fAdmin} ) ) {
        push @ds, $self->{p}->{fAdmin}, $self->{p}->{jj},
          $self->{se}->{line_from_page};
        $cl = 'AND t2.name = ?';
    }
    else {
        push @ds, $self->{p}->{jj}, $self->{se}->{line_from_page};
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT t1.action, 
                                t2.name,
                                t1.created,
                                t1.ip  
                                FROM log_smail AS t1,
                                     users AS t2
                                WHERE t2.id = t1.userid 
                                $cl
                                ORDER BY t1.created desc
                                LIMIT ?, ?
                             "
    );
    $sth->execute(@ds);

    my $loop_data;

    push @{$loop_data}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    foreach $_ ( @{$loop_data} ) {

        my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
          localtime( $_->{created} );
        $mon++;
        $year += 1900;
        if ( $mday < 10 ) { $mday = '0' . $mday; }
        if ( $mon < 10 )  { $mon  = '0' . $mon; }
        if ( $min < 10 )  { $min  = '0' . $min; }
        $_->{created} =
            $year . '-' 
          . $mon . '-' 
          . $mday . ' ' 
          . $hour . ':' 
          . $min . ':'
          . $sec;
    }

    if ( !$loop_data ) {
        $self->{he}->{NOTS} = 1;
    }
    else {
        $self->{he}->{'ROWS'} = $loop_data;
    }

    if ( !$self->{p}->{fAdmin} ) {
        $self->{p}->{fAdmin} = 'All';
    }
    $self->{he}->{ADMIN_S} = $self->{p}->{fAdmin};

    $self->{he}->{ADMIN} = \@admin2;

    return $self;
}

sub templ_hash_logms_list {
    my $self = shift;

    $self->{session}->{filet} = 'logsm';
    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
